package com.team.ai.service;

import com.team.ai.entity.ResultEntity;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ChatGPTService {
    @Value("${project.chatgptmodel}")
    private String openAiModel;
    @Resource
    private OpenAiService openAiService;

    /**
     * 提问
     * @param q 问题
     * @return String(JSON)
     */
    public String Chat(String q){
        CompletionRequest completionRequest = CompletionRequest.builder()
                .prompt(q)
                .model(openAiModel)
                .echo(true)
                .temperature(0.7)
                .topP(1d)
                .frequencyPenalty(0d)
                .presencePenalty(0d)
                .maxTokens(1000)
                .build();
        CompletionResult completionResult = openAiService.createCompletion(completionRequest);
        return ResultEntity.success(completionResult.getChoices().get(0).getText());
    }

}
